package com.name.givename.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.name.givename.dao.UserBonusInfoEntityMapper;
import com.name.givename.dto.BonusInfoDTO;
import com.name.givename.entity.UserBonusInfoEntity;
import com.name.givename.enums.ResultEnum;
import com.name.givename.exception.ServiceException;
import com.name.givename.param.PageParams;
import com.name.givename.param.UserOpenRedpackParams;
import com.name.givename.entity.UserAndBonusEntity;
import com.name.givename.result.ResultVoUtils;
import com.name.givename.service.UserBonusInfoService;
import com.name.givename.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.List;

/**
 * Created by yangtianrui on 18/9/2.
 */
@RestController
@RequestMapping("/userBonus")
@Api("用户红包中心")
public class UserBonusInfoController {

    @Autowired private UserBonusInfoService userBonusInfoService;
    @Autowired private UserBonusInfoEntityMapper userBonusInfoEntityMapper;

    @ApiOperation("初始化红包【前端可不用管】")
    @PostMapping("/initRedPack")
    public List<BonusInfoDTO> redPackList() {
        return userBonusInfoService.sendRedPack();
    }

    @ApiOperation("获取红包")
    @PostMapping("/getRedPack")
    public Result<List<BonusInfoDTO>>getRedPack(@RequestBody UserOpenRedpackParams userOpenRedpackParams) {
        List<UserAndBonusEntity>  count = userBonusInfoService.getBonusCount(userOpenRedpackParams.getOpenId());
        if (count.size() >= 3) {
              return ResultVoUtils.error(1,"你获取红包次数已用完");
        }
        return userBonusInfoService.getRedPack(userOpenRedpackParams);
    }
    /**
     * 提现排行榜
     */
    @ApiOperation("提现排行榜")
    @PostMapping("/getUserAndBonus")
    public Result<Page<UserAndBonusEntity>> getUserAndBonus(@RequestBody PageParams pageParams){
        //  此处有坑 PageHelper 一定要在获取集合前设置 不然分页失败
        PageHelper.startPage(pageParams.getPageNumber() == 0 ? 1 : pageParams.getPageNumber(),pageParams.getPageSize() == 0 ? 10 : pageParams.getPageSize());
        List<UserAndBonusEntity> list = userBonusInfoService.getUserAndBonus();
        list.stream().filter(x->x.getWinMoney() != null).limit(50);
        PageInfo pageInfo = new PageInfo(list);
        return ResultVoUtils.success(pageInfo);
    }

}
